/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2017年6月25日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.thread.wait_notify.example2_transformers_error;

/**
 * @Type EnergyTransferTask
 * @Desc
 * @author boboge
 * @date 2017年6月25日
 * @Version V1.0
 */
public class EnergyTransferTask implements Runnable {

    private EnergySystem energySystem;
    private int fromBox;
    private double maxAmount;
    private int DELAY = 10;

    /**
     * 
     */
    public EnergyTransferTask(EnergySystem energySystem, int from, double max) {
        this.energySystem = energySystem;
        this.fromBox = from;
        this.maxAmount = max;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            while (true) {
                int toBox = (int) (energySystem.getBoxAmount() * Math.random());
                double amount = maxAmount * Math.random();
                energySystem.transfer(fromBox, toBox, amount);
                Thread.sleep((long) (DELAY * Math.random()));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
